//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CDynamicVertex10.cpp>
///	@path	~/src/graphics/dx10/
///	@date	2009/01/10
///	@desc	.

#include "config/config.h"

#include "graphics/dx10/CDynamicVertex10.h"

namespace xeres {

	IMPL_IOBJECT_CLASS( CDynamicVertex10 );

	// CDynamicVertex10
	CDynamicVertex10::CDynamicVertex10( ID3D10Buffer * buffer , size_t count , size_t stride )
		: CBaseVertex10( buffer , count , stride )
	{
	}
	// ~CDynamicVertex10
	CDynamicVertex10::~CDynamicVertex10( void )
	{
	}
	// Map
	bool CDynamicVertex10::Map( bool discard , ResourceMap& data )
	{
		if( m_pBuffer == NULL )
			return false;

		if( ( m_desc.CPUAccessFlags & D3D10_CPU_ACCESS_WRITE ) == 0 )
		{
			TRACE_ERROR( _S("CDynamicVertex10::Map: Vertex buffer cannot be accessed by CPU.") );
			return false;
		}

		void * pData = NULL;
		HRESULT hr = m_pBuffer->Map( discard ? D3D10_MAP_WRITE_DISCARD : D3D10_MAP_WRITE , 0 , &pData );
		if( FAILED(hr) )
		{
			TRACE_ERROR( _S("CDynamicVertex10::Map: Failed to lock vertex buffer: %s") ,
				d3d_get_err(hr) );
			return false;
		}
		data.Map( (byte*)pData , m_desc.ByteWidth , m_stride , m_vertexCount );
		return true;
	}
	// ReadBack
	bool CDynamicVertex10::ReadBack( ResourceMap& data , bool readonly )
	{
		TRACE_ERROR( _S("CDynamicVertex10::ReadBack: Constant vertex is not lockable for read.") );
		return false;
	}
	// Unmap
	bool CDynamicVertex10::Unmap( void )
	{
		if( m_pBuffer == NULL )
			return false;

		m_pBuffer->Unmap();
		return true;
	}
	
} // namespace xeres
